package club.kingyin.kyblogprocore.service.impl;

import club.kingyin.commons.entity.core.Document;
import club.kingyin.commons.entity.core.Tag;
import club.kingyin.kyblogprocore.dao.DocumentDao;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import club.kingyin.kyblogprocore.dao.TagDao;
import club.kingyin.kyblogprocore.service.TagService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * (Tag)表服务实现类
 *
 * @author makejava
 * @since 2022-02-16 18:22:14
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagDao, Tag> implements TagService {

    @Resource
    private TagDao tagDao;

    @Resource
    private DocumentDao documentDao;

    @Override
    public List<Tag> getByUserId(int id) {
        LambdaQueryWrapper<Tag> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Tag::getUserid,id).select(Tag::getName,Tag::getId);
        return tagDao.selectList(queryWrapper);
    }

    public List<Tag> getTagsAndCountByUserId(int userId) {
        LambdaQueryWrapper<Document> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(Document::getTag).eq(Document::getUserid,userId);
        List<Tag> tags = getByUserId(userId);
        Map<String,Integer> tagInfo = new HashMap<>(tags.size());
        for (Tag tag : tags) {
            tagInfo.put(String.valueOf(tag.getId()),0);
        }
        // 计算每个标签含有文章总数
        for (Document document : documentDao.selectList(queryWrapper)) {
            if (document.getTag().isEmpty())
                continue;
            for (String s : document.getTag().split("-")) {
                tagInfo.put(s,tagInfo.get(s)+1);
            }
        }
        // 把原来userid替换成关联文章数量
        for (Tag tag : tags) {
            tag.setUserid(tagInfo.get(String.valueOf(tag.getId())));
        }
        return tags;
    }

    @Override
    public boolean containsName(int userid, String name) {
        LambdaQueryWrapper<Tag> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Tag::getName, name).eq(Tag::getUserid, userid).select(Tag::getId);
        return tagDao.selectCount(queryWrapper) > 0;
    }

    public boolean removeTagFromDocumentByTagId(int userid, int id) {
        LambdaQueryWrapper<Tag> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Tag::getId, id).eq(Tag::getUserid, userid);
        int delete = tagDao.delete(queryWrapper);
        LambdaUpdateWrapper<Document> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.setSql("tag=REPLACE(tag,'" + id + "-'," + "'')").eq(Document::getUserid, userid).like(Document::getTag,id+'-');
        documentDao.update(null,updateWrapper);
        return delete > 0;
    }

    public List<String> getTagsByTag(String tag) {
        List<String> ts = new ArrayList<>();
        LambdaQueryWrapper<Tag> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(Tag::getId,Arrays.asList(tag.split("-"))).select(Tag::getName);
        for (Tag t : tagDao.selectList(queryWrapper)) {
            ts.add(t.getName());
        }
        return ts;
    }

}

